﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Minecraft_Xenon
{
    public partial class Setting : Form
    {
        public Setting()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 163 && this.ClientRectangle.Contains(this.PointToClient(new Point(m.LParam.ToInt32()))) && m.WParam.ToInt32() == 2)
                m.WParam = (IntPtr)1;
            base.WndProc(ref m);
            if (m.Msg == 132 && m.Result.ToInt32() == 1)
                m.Result = (IntPtr)2;
        }
        private void Setting_Load(object sender, EventArgs e)
        {
            IniFile ini = new IniFile(Application.StartupPath + @"\config");
            if (ini.ReadInivalue("Main", "mcpath") == "")
            {
                textBox1.Text = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\.minecraft";
            }
            else
            {
                textBox1.Text = ini.ReadInivalue("Main", "mcpath");
            }

            if (ini.ReadInivalue("Main", "lctype") == "1")
            {
                radioButton2.Checked = true;
            }
            else
            {
                radioButton1.Checked = true;
            }

            if (radioButton2.Checked == true)
            {
                label4.Text = "正版/第三方启动器文件名称：(请使用绝对路径)";
                textBox3.Visible = true;
                textBox2.Visible = false;
                linkLabel4.Visible = true;
            }
            else
            {
                label4.Text = "启动参数： ( -Xmx 最大内存 , -Xms 初始内存 , 最后的字符串为ID)";
                textBox2.Visible = true;
                textBox3.Visible = false;
                linkLabel4.Visible = false;
            }

            if (ini.ReadInivalue("Main", "lcextracommand") != "")
            {
                textBox2.Text = ini.ReadInivalue("Main", "lcextracommand");
            }
            else
            {
                //textBox2.Text = "-Xms512m -Xmx1024m {DO} net.minecraft.client.Minecraft Player";
                textBox2.Text = "";
            }
            
            textBox3.Text = ini.ReadInivalue("Main", "lcpath");

            textBox4.Text = ini.ReadInivalue("Misc","lcxms");
            textBox5.Text = ini.ReadInivalue("Misc","lcxmx");
            textBox6.Text = ini.ReadInivalue("Misc", "lcuser");
        }


        //-cp "%APPDATA%\.minecraft\bin\*" -Djava.library.path="%APPDATA%\.minecraft\bin\natives"
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
            {
                label4.Text = "正版/第三方启动器文件名称：(请使用绝对路径)";
                textBox3.Visible = true;
                textBox2.Visible = false;
                linkLabel4.Visible = true;
            }
            else
            {
                label4.Text = "启动参数（可留空）";
                //textBox2.Text = "-Xms512m -Xmx1024m {DO} net.minecraft.client.Minecraft Player";
                textBox2.Visible = true;
                textBox3.Visible = false;
                linkLabel4.Visible = false;
            }
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (textBox1.Text == ""||System.IO.File.Exists(textBox1.Text + @"\bin\minecraft.jar") == false)
            {
                MessageBox.Show("请检查Minecraft目录！");
                return;
            }

            /*
            if (textBox2.Text == "" && radioButton1.Checked == true)
            {
                MessageBox.Show("请检查Minecraft启动参数！");
                return;
            }
            */

            if (textBox3.Text == "" && radioButton2.Checked == true)
            {
                MessageBox.Show("请检查Minecraft启动器目录！");
                return;
            }

            //在MC目录下创建ini文件
            IniFile ini = new IniFile(Application.StartupPath + @"\config");
            //写入MC目录
            ini.WriteInivalue("Main", "mcpath", textBox1.Text);
            //当使用内置启动时写入
            if (radioButton1.Checked == true)
            {
                ini.WriteInivalue("Main", "lctype", "0");
            }
            else
            {
                ini.WriteInivalue("Main", "lctype", "1");
            }
            //写入内置启动命令
            //ini.WriteInivalue("Main", "lccommand", textBox2.Text);
            ini.WriteInivalue("Main", "lccommand", 
                "-Xms" 
                +textBox4.Text 
                +"m -Xmx"+
                textBox5.Text+
                "m {DO} net.minecraft.client.Minecraft " +
                textBox6.Text);
            //写入附加
            ini.WriteInivalue("Main", "lcextracommand", textBox2.Text);
            //写入第三方启动器路径
            ini.WriteInivalue("Main", "lcpath", textBox3.Text);

            //写入杂项数据
            ini.WriteInivalue("Misc", "lcxms",textBox4.Text);
            ini.WriteInivalue("Misc", "lcxmx",textBox5.Text);
            ini.WriteInivalue("Misc", "lcuser",textBox6.Text);

            this.Close();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            folderBrowserDialog1.Description = "Minecraft 位置 ( .minecraft 目录 )";
            if(folderBrowserDialog1.ShowDialog() == DialogResult.OK){
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = openFileDialog1.FileName;
            }
        }

        private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://mcspace.info");
        }
    }
}
